<?php 
Class wxruleController extends appController
{
	
	public function index()
	{
	   	$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$menu_list=Model('wxrule')->get_rule_list($page,10);
		$this->assign('ur_here','>规则列表');
		
		//$this->assign('search_action','index.php?g=admin&m=menu&a=search');
		$this->assign('menu_list',$menu_list['data']);
		$this->assign('menu_page',$menu_list['pagelink']);
		
		
		$this->display('rule_list.html');
	
	}
	public function save()
	{
		$info['token']=isset($_POST['token'])?$_POST['token']:'';
		$info['appid']=isset($_POST['appid'])?$_POST['appid']:'';
		$info['appsecret']=isset($_POST['appsecret'])?$_POST['appsecret']:'';
		$info['wxareply']=isset($_POST['wxareply'])?intval($_POST['wxareply']):'';
		$info['wxdreply']=isset($_POST['wxdreply'])?intval($_POST['wxdreply']):'';
		
		$data=array('error'=>1,'content'=>Lang('INVALID_OPERATION'));
		if(Model('wxrule')->saveWeixin($info))
		{
		  $data['error']=0;
		  $data['content']=Lang('VALID_OPERATION');
		}
		else
		{
		}
		$this->showMsg($data);
	}
	public function weixin()
	{
		
		$menu_list=Model('wxrule')->get_rule_list();
		$this->assign('rule_list',$menu_list);
		$setting=Model('wxrule')->getWeixin();
		$this->assign('ur_here','>微信设置');
		$this->assign('setting',$setting);
		$this->display('weixin.html');
	
	}
	
	public function add_rule()
	{
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		$art_list=Model('article')->get_all_art();
		$this->assign('art_list',$art_list);
		$this->assign('ur_here','>添加规则');
		$this->assign('action','create_rule');
		$this->display('rule.html');
	
	}
	
	public function create_rule()
	{
		$img_arr=array();
		$i=0;
		foreach($_POST as $k=>$v)
		{
			$i++;
			if(strstr($k,'field'))
			{
				$img_arr[$i]['title']=$v[0];
				$img_arr[$i]['description']=$v[1];
				$img_arr[$i]['url']=$v[2];
				$img_arr[$i]['thumb']=$v[3];
			}
		}
		
		$info['name']=isset($_POST['name'])?$_POST['name']:'';
		$info['createtime']=$_SERVER['REQUEST_TIME'];
	    $info['type']=isset($_POST['type'])?intval($_POST['type']):1;
	    $keyword=isset($_POST['keyword'])?$_POST['keyword']:'';
	
		$data=array('error'=>0,'content'=>'','url'=>'');
		if($info['name']=='')
		{
			$data['error']=1;
			$data['content']='标题不能为空';
			$this->showMsg($data);
		}
		if($_POST['type']==1)
		{
		  	
		  	$content=isset($_POST['content'])?$_POST['content']:'';
		}
		else 
		{
			$content=$img_arr;
		}
		
		if(!Model('wxkey')->check_key($keyword)&&$keyword!='')
		{
			$data['error']=1;
			$data['content']='关键词已经存在了';
			$this->showMsg($data);
		}
	
		   $result=Model('wxrule')->insert_rule($info,$content);
		  //快速绑定关键词
		   if($result['rule_id']>0&&$keyword!='')
		   {
		   	  $temp=array('keyword'=>$keyword,'rid'=>$result['rule_id'],'type'=>'0','status'=>'1');
		   	  
		   	  	Model('wxkey')->create_key($temp);
		   	  
		   	 
		   }
		   
		     if($result)
		   {
		   	
			$data['content']=Lang('SUCCESS_ADD');
			$data['url']="index.php?g=admin&m=wxrule&a=index";
			
		   }
		   else
		  {
		  	$data['error']=1;
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		   }
		   $this->showMsg($data);
		
		
	} 
	public function del()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
			//$result=Model('menu')->del_menu($v);
			}
		}
		else 
		{
			$result=Model('wxrule')->del_rule($id);
		}
		print_r(json_encode($result));
	}
	
	public function file()
	{
	
		$result=array('error'=>0,'url'=>$_FILES['thumb']['name']);
		$file=current($_FILES);
		$thumb_url=Model('image')->upload_image($file, 'rule/'.date('Ym'));
		
		$result['url']=$thumb_url;
	
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
	public function edit_rule()
	{
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$menu_info=Model('wxrule')->get_rule_info($id);
		$art_list=Model('article')->get_all_art();
		$this->assign('art_list',$art_list);
		$this->assign('rule',$menu_info);
		$this->assign('action','update_rule');
		$this->display('rule.html');
	
	}
	public function delimg()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$res=Model('wxrule')->del(array('table'=>'wxrule','id'=>$id));
		$result=array('error'=>1,'content'=>Lang('INVALID_OPERATION'));
		if($res)
		{
			$result['error']=0;
		}
		
		
		print_r(json_encode($result));
		exit();
	}
	public function update_rule()
	{
		$img_arr=array();
		$i=0;
		foreach($_POST as $k=>$v)
		{
			$i++;
			if(strstr($k,'field')&&$v[4]<=0)
			{
				$img_arr[$i]['title']=$v[0];
				$img_arr[$i]['description']=$v[1];
				$img_arr[$i]['url']=$v[2];
				$img_arr[$i]['thumb']=$v[3];
			}
		}
	
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$info['name']=isset($_POST['name'])?$_POST['name']:'';
		$info['createtime']=$_SERVER['REQUEST_TIME'];
		$info['status']=isset($_POST['is_show'])?intval($_POST['is_show']):0;
	//	$info['type']=isset($_POST['type'])?intval($_POST['type']):1;
		
		
		$data=array('error'=>0,'content'=>'','url'=>'');
		if($info['name']=='')
		{
			$data['error']=1;
			$data['content']='标题不能为空';
			$this->showMsg($data);
		}
		
		if($_POST['type']==1)
		{
			
			$content=isset($_POST['content'])?$_POST['content']:'';
		}
		else
		{
			$content=$img_arr;
		}
		
			$result=Model('wxrule')->update_rule($info,$content,$id);
		
			if($result)
			{
				$data['content']=Lang('SUCCESS_EDIT');
				$data['url']="index.php?g=admin&m=wxrule&a=index";
					
			}
			else
			{
				$data['error']=1;
				$data['content']=Lang('INVALID_OPERATION');
				//$data['url']=$_SERVER['REQUEST_URI'];
			}
			$this->showMsg($data);
		
		
	}
	
	
}